package com.springboot.film.controller;

import com.springboot.film.entity.Page;
import com.springboot.film.entity.vipinfo;
import com.springboot.film.service.vipinfoService;
import net.sf.json.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.ArrayList;
import java.util.List;

@Controller
public class vipinfoCtrl {

    @Autowired
    private vipinfoService vipinfoService;

    //分页显示全部
    @RequestMapping("/houtai/vipinfo")
    public String vipinfo(Model model, String pageNow, String nosearch) {
        int totalCount = 0;
        totalCount = vipinfoService.getCount();
        List<vipinfo> vipinfoList=new ArrayList<vipinfo>();
        Page page=null;
        if(pageNow!=null){
            page=new Page(totalCount, Integer.parseInt(pageNow));
            vipinfoList=vipinfoService.findByPage(page.getStartPos(), page.getPageSize());
        }else{
            page=new Page(totalCount, 1);//初始化pageNow为1
            vipinfoList=vipinfoService.findByPage(page.getStartPos(), page.getPageSize());
        }
        model.addAttribute("page", page);
        model.addAttribute("top", "top_vipinfo");
        model.addAttribute("show_left", "left_vipinfo");
        model.addAttribute("left", "left_y_vipinfo");
        model.addAttribute("mypage", "vipinfo");
        model.addAttribute("vipinfo", vipinfoList);
        if(nosearch != null){
            model.addAttribute("nosearch", nosearch);
        }
        return "houtai_vipinfo";
    }

    //分页显示全部搜索
    @RequestMapping("/houtai/searchvipinfo")
    public String searchvipinfo(Model model, String pageNow,int types,String keyword) {
        int totalCount = 0;
        String keyword_new="b.name";
        if(types == 1) {
            keyword_new = "b.name";
        }else if (types == 2) {
            keyword_new = "DATEDIFF(FROM_UNIXTIME(a.end_time, '%Y-%m-%d'),FROM_UNIXTIME(a.start_time, '%Y-%m-%d'))";
        }
        totalCount = vipinfoService.getsearchCount(keyword_new,keyword);
        List<vipinfo> vipinfoList=new ArrayList<vipinfo>();
        Page page=null;
        if(pageNow!=null){
            page=new Page(totalCount, Integer.parseInt(pageNow));
            vipinfoList=vipinfoService.findsearchByPage(keyword_new,keyword,page.getStartPos(), page.getPageSize());
        }else{
            page=new Page(totalCount, 1);//初始化pageNow为1
            vipinfoList=vipinfoService.findsearchByPage(keyword_new,keyword,page.getStartPos(), page.getPageSize());
        }
        if(vipinfoList != null && totalCount != 0){
            model.addAttribute("page", page);
            model.addAttribute("top", "top_vipinfo");
            model.addAttribute("show_left", "left_vipinfo");
            model.addAttribute("left", "left_y_vipinfo");
            model.addAttribute("mypage", "search&types="+types+"&keyword="+keyword+"");
            model.addAttribute("vipinfo", vipinfoList);
            return "houtai_vipinfo";
        }else{
            model.addAttribute("nosearch", "nosearch");
            return "redirect:/houtai/vipinfo?nosearch=nosearch";
        }
    }

    //添加
    //接收并返回json格式数据
    @RequestMapping("/houtai/addvipinfo")
    @ResponseBody
    public String addvipinfo(vipinfo vipinfo) {
        vipinfoService.add(vipinfo);
        vipinfoService.updatevip(vipinfo);
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("code", "1");
        return jsonObject.toString();
    }

    //更改
    @RequestMapping("/houtai/updatevipinfo")
    public String updatevipinfo(vipinfo vipinfo) {
        vipinfoService.update(vipinfo);
        vipinfoService.updatevip(vipinfo);
        return "redirect:/houtai/vipinfo";
    }

    //删除
    @RequestMapping("/houtai/deletevipinfo")
    public String deletevipinfo(int userid) {
        vipinfoService.updatevipdelete(userid);
        vipinfoService.delete(userid);
        return "redirect:/houtai/vipinfo";
    }

    //跳转到更改页面
    @RequestMapping("/houtai/to_updatevipinfo")
    public String to_updatevipinfo(Model model, int userid) {
        List<vipinfo> vipinfoList=new ArrayList<vipinfo>();
        vipinfoList = vipinfoService.getUserone(userid);
        model.addAttribute("top", "top_vipinfo");
        model.addAttribute("show_left", "left_updatevipinfo");
        model.addAttribute("left", "left_y_vipinfo");
        model.addAttribute("mypage", "vipinfo");
        model.addAttribute("vipinfo", vipinfoList);
        return "houtai_updatevipinfo";
    }

    //跳转到添加页面
    @RequestMapping("/houtai/to_addvipinfo")
    public String to_addvipinfo(Model model) {
        model.addAttribute("top", "top_vipinfo");
        model.addAttribute("show_left", "left_addvipinfo");
        model.addAttribute("left", "left_y_vipinfo");
        model.addAttribute("mypage", "vipinfo");
        return "houtai_addvipinfo";
    }

    //每天自动查找是否过期vip
    @RequestMapping("/updatevip")
    public void updatevip() {
        int max_vip=0;
        int i=1;
        max_vip=vipinfoService.search_id();
        for(i=1;i<=max_vip;i++) {
            vipinfoService.updatevipday(i);
        }
    }

}
